Có cách nào để kiểm tra tính toàn vẹn của tham chiếu cho các bảng MyIsam bằng cách sử dụng quan hệ gốc YII không? (Is there a way to check referential integrity for MyIsam tables using YII native relations?)


問題描述

Có cách nào để kiểm tra tính toàn vẹn của tham chiếu cho các bảng MyIsam bằng cách sử dụng quan hệ gốc YII không? (Is there a way to check referential integrity for MyIsam tables using YII native relations?)

While I used InnoDB tables I caught FK constraint exceptions from InnoDB engine.

I know that Yii can fetch related tables according to relations(). Besides I need to check referential integrity during other CRUD operations ‑ such as insert, update and delete.

Is there some Yii native mechanism to perform this? Or I should write additional code for this checking?


參考解法

方法 1:

In order to avoid errors from the database you need to write additional code to handle this. I manage it using the beforeDelete() method of my models. Write an if condition to check for referential integrity violations uising the model's relations. You can then either delete the offending records or abort the deletion by not returning 'parent::beforeDelete().'

In this many to many example from a User model we can wrap the 'return parent::beforeDelete()' call in an if to check the relations() of the model in the following way:

 protected function beforeDelete() {
            if (empty($this‑>tasks))
            return parent::beforeDelete();
        }

The deletion is aborted if the User is found to have any assigned tasks. 

For reference the relation in the model would look like this and requires a third table:

'tasks' => array(self::MANY_MANY, 'Task', 'task_assignment(user_id,task_id)'),

This example is very basic and obviously you may want to implement a more complex way of handling these scenarios which could include deletion of related records or even the updating of them to remove their foreign key references. You will have to decide what works best and makes sense for each of your models and their relations.

(by Peter LyashevichShaunUK)

參考文件

  1. Is there a way to check referential integrity for MyIsam tables using YII native relations? (CC BY‑SA 3.0/4.0)

#yii #referential-integrity #myisam






相關問題

Yii:如何在 main.php(配置文件)中設置佈局? (Yii: how to set layout in main.php (config file)?)

YII - mengirim surat dengan sendGrid (YII - mailing with sendGrid)

Dữ liệu trong Yii, Sử dụng Ajax (Datatables in Yii, Using Ajax)

插入新元素後更新 Yii 下拉列表 (Updating Yii Dropdownlist After Insert New Element)

yii 中的 Self::Many 關係 (Self::Many relation in yii)

為什麼 Yii2 在 yii\caching\Cache 中使用縮寫的方法名? (Why Yii2 uses abbreviated methods names in yii\caching\Cache?)

YIi PHP - 帶有 foreach 循環的輸出數組 (YIi PHP - Output array with a foreach loop)

學習 Yii2:數據轉換 (Learning Yii2: Data Transformation)

Chtml::link 與 JQuery (Chtml::link with JQuery)

使用臭名昭著的 CActiveRecord 在 Yii 框架中一次查找多條記錄? (Finding multiple records at once in Yii Framework using the infamous CActiveRecord?)

在 Yii2 中獲取當前網頁的完整 url (Get full url of current webpage in Yii2)

如何從php中的數組中刪除空白值 (how to remove blank values in from array in php)







留言討論